package com.frcdi.qms.measurement.service.impl;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.measurement.domain.MeasurementBalance;
import com.frcdi.qms.measurement.domain.MeasurementRecord;
import com.frcdi.qms.measurement.mapper.MeasurementBalanceMapper;
import com.frcdi.qms.measurement.mapper.MeasurementRecordMapper;
import com.frcdi.qms.measurement.service.IMeasurementRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 一般性测量数据采集记录Service业务层处理
 *
 * @author cell
 * @date 2024-02-27
 */
@Service
public class MeasurementRecordServiceImpl implements IMeasurementRecordService
{
    @Autowired
    private MeasurementRecordMapper measurementRecordMapper;

    @Autowired
    private MeasurementBalanceMapper balanceMapper;

    /**
     * 查询一般性测量数据采集记录
     *
     * @param recordId 一般性测量数据采集记录主键
     * @return 一般性测量数据采集记录
     */
    @Override
    public MeasurementRecord selectMeasurementRecordByRecordId(Long recordId)
    {
        return measurementRecordMapper.selectMeasurementRecordByRecordId(recordId);
    }

    /**
     * 查询一般性测量数据采集记录列表
     *
     * @param measurementRecord 一般性测量数据采集记录
     * @return 一般性测量数据采集记录
     */
    @Override
    public List<MeasurementRecord> selectMeasurementRecordList(MeasurementRecord measurementRecord)
    {
        return measurementRecordMapper.selectMeasurementRecordList(measurementRecord);
    }

    /**
     * 新增一般性测量数据采集记录
     *
     * @param measurementRecord 一般性测量数据采集记录
     * @return 结果
     */
    @Override
    public int insertMeasurementRecord(MeasurementRecord measurementRecord)
    {
        return measurementRecordMapper.insertMeasurementRecord(measurementRecord);
    }

    /**
     * 修改一般性测量数据采集记录
     *
     * @param measurementRecord 一般性测量数据采集记录
     * @return 结果
     */
    @Override
    public int updateMeasurementRecord(MeasurementRecord measurementRecord)
    {
        return measurementRecordMapper.updateMeasurementRecord(measurementRecord);
    }

    /**
     * 批量删除一般性测量数据采集记录
     *
     * @param recordIds 需要删除的一般性测量数据采集记录主键
     * @return 结果
     */
    @Override
    public int deleteMeasurementRecordByRecordIds(Long[] recordIds)
    {
        return measurementRecordMapper.deleteMeasurementRecordByRecordIds(recordIds);
    }

    /**
     * 删除一般性测量数据采集记录信息
     *
     * @param recordId 一般性测量数据采集记录主键
     * @return 结果
     */
    @Override
    public int deleteMeasurementRecordByRecordId(Long recordId)
    {
        return measurementRecordMapper.deleteMeasurementRecordByRecordId(recordId);
    }

    @Override
    public int reMeasure(MeasurementRecord measurementRecord) {
        // 修改之前记录为历史数据
        measurementRecordMapper.updateMeasurementRecord(measurementRecord);
        MeasurementBalance measurementBalance = balanceMapper.selectMeasurementBalanceByMeasurementId(measurementRecord.getMeasurement());
        // 增加新记录
        MeasurementRecord endRecord = new MeasurementRecord();
        endRecord.setMeasurement(measurementRecord.getMeasurement());
        endRecord.setMeasurementNumber(measurementRecord.getMeasurementNumber());
        endRecord.setDataNumber(measurementRecord.getDataNumber()+1);
        endRecord.setRecordData(measurementRecord.getRecordData());
        if (measurementRecord.getRecordData().compareTo(measurementBalance.getMinimum())>=0
                && measurementRecord.getRecordData().compareTo(measurementBalance.getMaximum())<=0){
            endRecord.setIsRange(UserConstants.IS_NOT_OVERFLOW);
        }else {
            endRecord.setIsRange(UserConstants.IS_OVERFLOW);
        }

        return measurementRecordMapper.insertMeasurementRecord(endRecord);
    }
}
